package com.lxujia.ruankao.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;

import com.lxujia.ruankao.adapter.MainFragmentPagerAdapter;
import com.lxujia.ruankao.fragment.AboutFragment;
import com.lxujia.ruankao.fragment.ErrorQuestionFragment;
import com.lxujia.ruankao.fragment.OldExamsFragment;
import com.lxujia.ruankao.R;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends RuanKaoBaseActivity {
    private Fragment mOldExamsFragment;
    private Fragment mErrorQuestionFragment;
    private Fragment mAboutFragment;
    private List<Fragment> mFragments = new ArrayList<Fragment>();
    private ViewPager mViewPager;
    private BottomNavigationView mNavigation;

    private final static int EXAMS_FRAGMENT_POSITION = 0;
    private final static int ERROR_FRAGMENT_POSITION = 1;
    private final static int ABOUT_FRAGMENT_POSITION = 2;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_old_exams:
                    mViewPager.setCurrentItem(EXAMS_FRAGMENT_POSITION);
                    return true;
                case R.id.navigation_error_questions:
                    mViewPager.setCurrentItem(ERROR_FRAGMENT_POSITION);
                    return true;
                case R.id.navigation_about:
                    mViewPager.setCurrentItem(ABOUT_FRAGMENT_POSITION);
                    return true;
            }
            return false;
        }
    };

    private ViewPager.OnPageChangeListener mOnPageChageListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            Menu item = mNavigation.getMenu();
            item.getItem(position).setChecked(true);

            // 移动到关于时，修改title
            if (position == ABOUT_FRAGMENT_POSITION) {
                setTitle(R.string.title_about);
            } else {
                setTitle(R.string.app_name);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {
        initViewPager();
        initNavigationView();
    }

    private void initViewPager() {
        initFragment();

        mViewPager = (ViewPager) findViewById(R.id.main_fragment);
        mViewPager.setAdapter(new MainFragmentPagerAdapter(getSupportFragmentManager(), mFragments));
        mViewPager.addOnPageChangeListener(mOnPageChageListener);
        mViewPager.setCurrentItem(0);
    }

    private void initNavigationView() {
        mNavigation = (BottomNavigationView) findViewById(R.id.navigation);
        mNavigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }

    private void initFragment() {
        mOldExamsFragment = new OldExamsFragment();
        mErrorQuestionFragment = new ErrorQuestionFragment();
        mAboutFragment = new AboutFragment();

        mFragments.add(mOldExamsFragment);
        mFragments.add(mErrorQuestionFragment);
        mFragments.add(mAboutFragment);
    }
 }
